<template>
  <UploadExcel :on-success="upload" />
</template>

<script>
import { importEmployee } from '@/api/employeeSimple'
export default {

  methods: {
    // 获取后台数据 定义字典
    async upload(data) {
      // console.log(1)
      console.log(data)
      const dict = {
        '入职日期': 'timeOfEntry',
        '手机号': 'mobile',
        '姓名': 'username',
        '转正日期': 'correctionTime',
        '工号': 'workNumber'
      }
      const newArray = data.results.map(user => {
        return this.zh2en(user, dict)
      })
      // console.log(newArray)
      await importEmployee(newArray)
      // 提示
      this.$message.success('操作成功')
      // 跳转回上一个页面
      this.$router.back()
    },
    // 转换为英文
    zh2en(user, dict) {
      const newUser = {}
      // 遍历每一个员工数据
      for (const key in user) {
        // console.log(user[key])
        // 中文转换英文
        const enkey = dict[key]
        // 判断是否为时间数据
        if (enkey === 'timeOfEntry' || enkey === 'correctionTime') {
          console.log(1)
          const timeStr = this.formatDate(user[key], '/')
          const date = new Date(timeStr)
          newUser[enkey] = date
        } else {
          newUser[enkey] = user[key]
        }
      }
      return newUser
    },
    // 时间过滤器
    formatDate(numb, format) {
      const time = new Date((numb - 1) * 24 * 3600000 + 1)
      time.setYear(time.getFullYear() - 70)
      const year = time.getFullYear() + ''
      const month = time.getMonth() + 1 + ''
      const date = time.getDate() - 1 + ''
      if (format && format.length === 1) {
        return year + format + month + format + date
      }
      return year + (month < 10 ? '0' + month : month) + (date < 10 ? '0' + date : date)
    }

  }
}
</script>

<style>

</style>
